package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateHomePageDTO;
import com.itlong.cloud.POJO.PO.HomePageInfoPO;
import com.itlong.cloud.POJO.VO.operate.OperateAppHomePageGetPageVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.operate.dao.IOperateAppHomePageDao;
import com.itlong.cloud.operate.service.IOperateAppHomePageService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 *      运营-APP-启动页管理业务处理接口实现类
 * </desc>
 *
 * @createDate 2019/06/24
 */
@Service
public class OperateAppHomePageServiceImpl implements IOperateAppHomePageService {
    @Autowired
    IOperateAppHomePageDao iOperateAppHomePageDao;

    /**
     * <desc>
     *      获取分页数据
     * </desc>
     *
     * @return page模型
     * @author Qiang.S
     * @createDate 2019/06/24
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateAppHomePageGetPageVO> getHomePage() throws Exception {
        //将DTO转换为Map params
        Map<String, Object> params = new HashMap<>();
        //分页类默认pageSize为20
        Page<OperateAppHomePageGetPageVO> page = new Page<>();
        page.getPage().setPageSize("15");
        page.getPage().setCurrentPage("1");
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        //填充返回数据集
        page.setRows(iOperateAppHomePageDao.getPage(params), iOperateAppHomePageDao.getPageCount(params));
        return page;
    }

    /**
     * <desc>
     *      添加启动页
     * </desc>
     *
     * @param dto 功能实体
     * @return sql执行行数
     * @author Qiang.S
     * @createDate 2019/06/24
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer save(OperateHomePageDTO dto) throws Exception {
        Date nowTime = new Date();
        HomePageInfoPO po = new HomePageInfoPO();
        BeanUtils.copyProperties(dto,po);
        po.setHomePageId(LogicIdUtil.bussinessId());
        po.setStatus(DataStatusEnum.NORMAL.getType());
        po.setCreateTime(nowTime);
        po.setUpdateTime(nowTime);
        po.setStartTime( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dto.getStartTime()));
        po.setEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dto.getEndTime()));
        return iOperateAppHomePageDao.save(SqlUtil.durableData(po, PlatformConstants.TABLE_SAVE));
    }

    /**
     * <desc>
     *      更新功能
     * </desc>
     *
     * @param dto 功能实体
     * @return sql执行行数
     * @author Qiang.S
     * @createDate 2019/06/24
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer update(OperateHomePageDTO dto) throws Exception {
        Date nowTime = new Date();
        HomePageInfoPO po = new HomePageInfoPO();
        BeanUtils.copyProperties(dto,po);
        po.setUpdateTime(nowTime);
        return iOperateAppHomePageDao.update(SqlUtil.durableData(po, PlatformConstants.TABLE_UPDATE));
    }

    /**
     * <desc>
     *      通过id获取功能
     * </desc>
     *
     * @param homePageId 启动页编号
     * @return 资源实体
     * @author Qiang.S
     * @createDate 2019/06/24
     */
    @Override
    public OperateAppHomePageGetPageVO getOne(String homePageId) {
        return iOperateAppHomePageDao.getOne(homePageId);
    }

    /**
     * <desc>
     *      删除功能
     * </desc>
     *
     * @param homePageId 启动页编号
     * @return sql执行行数
     * @author Qiang.S
     * @createDate 2019/06/24
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer delete(String homePageId) {
        return iOperateAppHomePageDao.delete(homePageId);
    }

    /**
     * <desc>
     *      获取启动页可用数据
     * </desc>
     *
     * @return page模型
     * @author Qiang.S
     * @createDate 2019/06/24
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public OperateAppHomePageGetPageVO StartAdvertising() {
        return iOperateAppHomePageDao.StartAdvertising();
    }
}
